function asdfdfgh() {
  axios({
    url: '/student/list',
    method: 'get',

  }).then(({
    data: res
  }) => {
    if (res.code == 0) {
      let arr = []
      res.data.forEach(element => {
        arr.push(`<tr>
        <th scope="row">${element.id}</th>
        <td>${element.name}</td>
        <td>${element.age}</td>
        <td>${element.sex}</td>
        <td> ${element.group} </td>
        <td>${element.phone} </td>
        <td>${element.salary}</td>
        <td>${element.truesalary}</td>
        <td>${element.province+element.city+element.county}</td>
        <td>
          <button type="button" data-id='${element.id}' class="update btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#updateModal">修改</button>
          <button type="button" data-id='${element.id}' class="delete btn-danger btn-sm">删除</button>
        </td>
      </tr>`)
      })
      document.querySelector('tbody').innerHTML = arr.join('')
    }
  });
}
asdfdfgh()
let tbody = document.querySelector('tbody')
tbody.addEventListener('click', function (e) {
  if (e.target.className.indexOf('delete') !== -1) {
    if (!confirm('确定删?')) return
    let id = e.target.getAttribute('data-id')
    axios({
      url: '/student/delete',
      method: 'delete',
      params: {
        id
      }
    }).then(({
      data: res
    }) => {
      if (res.code == 0) {
        toastr.success('删除成功')
        asdfdfgh()
      }
    });
  }
})
let form1 = document.querySelector('#addModal form')
let form2 = document.querySelector('#updateModal form')
yyds(form1)
yyds(form2)

function yyds(yyi) {
  axios({
    url: '/geo/province',
    method: 'get',
  }).then(({
    data: res
  }) => {
    let arr = [`<option  value="">--省--</option>`]
    res.forEach(element => {
      arr.push(`<option  value="${element}">${element}</option>`)
    });
    yyi.querySelector('[name="province"]').innerHTML = arr.join('')
  });


  yyi.querySelector('[name="province"]').onchange = function () {
    yyi.querySelector('[name="county"]').innerHTML = '<option selected value="">--县--</option>'
    if (this.value == '') {
      yyi.querySelector('[name="city"]').innerHTML = '<option selected value="">--市--</option>'
      return
    }
    axios({
      url: '/geo/city',
      method: 'get',
      params: {
        pname: this.value
      }
    }).then(({
      data: res
    }) => {
      let arr = [` <option selected value="">--市--</option>`]
      res.forEach(element => {
        arr.push(`<option selected value="${element}">${element}</option>`)
      });
      yyi.querySelector('[name="city"]').innerHTML = arr.join('')
    })
  }



  yyi.querySelector('[name="city"]').onchange = function () {
    if (this.value == '') {
      yyi.querySelector('[name="county"]').innerHTML = '<option selected value="">--县--</option>'
      return
    }
    axios({
      url: '/geo/county',
      method: 'get',
      params: {
        pname: yyi.querySelector('[name="province"]').value,
        cname: this.value
      }
    }).then(({
      data: res
    }) => {
      let arr = [` <option selected value="">--区/县--</option>`]
      res.forEach(element => {
        arr.push(`<option selected value="${element}">${element}</option>`)
      });
      yyi.querySelector('[name="county"]').innerHTML = arr.join('')
    })
  }
}
form1.onsubmit = function (e) {
  e.preventDefault();
  axios({
    url: '/student/add',
    method: 'POST',
    data: $(form1).serialize()

  }).then(({
    data: res
  }) => {
    if (res.code == 0) {
      toastr.success(res.message)
      document.querySelector('#addModal .btn-close').click()
      asdfdfgh()
    }
  });
}
tbody.addEventListener("click", function (e) {
  if (e.target.className.indexOf('update') !== -1) {
    let id = e.target.getAttribute('data-id')
    axios({
      url: '/student/one',
      method: 'get',

      params: {
        id
      }
    }).then(({
      data: res
    }) => {
      if (res.code == 0) {
        form2.querySelector('[name="name"]').value = res.data.name;
        form2.querySelector('[name="age"]').value = res.data.age;
        form2.querySelector('[name="phone"]').value = res.data.phone;
        form2.querySelector('[name="salary"]').value = res.data.salary;
        form2.querySelector('[name="truesalary"]').value = res.data.truesalary;
        let inps = form2.querySelectorAll('[name="sex"]')
        if (res.data.sex == '男') {
          inps[0].checked = true
          inps[1].checked = false
        } else {
          inps[0].checked = false
          inps[1].checked = true
        }

        let options1 = form2.querySelectorAll('[name=group] option')
        for (let i = 0; i < options1.length; i++) {
          if (options1[i].value == res.data.group) {
            options1[i].selected = true
          } else {
            options1[i].selected = false
          }
        }

        let options2 = form2.querySelectorAll('[name=province] option')
        for (let i = 0; i < options2.length; i++) {
          if (options2[i].value == res.data.province) {
            options2[i].selected = true
          } else {
            options2[i].selected = false
          }
        }
        form2.querySelector('[name=city]').innerHTML = ` <option selected value="${res.data.city}">${res.data.city}</option>`
        form2.querySelector('[name=county]').innerHTML = ` <option selected value="${res.data.county}">${res.data.county}</option>`
        form2.querySelector('[name="id"]').value = res.data.id
      }
    });
  }
})
form2.onsubmit = function (e) {
  e.preventDefault()
  axios({
    url: '/student/update',
    method: 'put',
    data: $(form2).serialize()
  }).then(({
    data: res
  }) => {

    if (res.code == 0) {
      toastr.success(res.message)
      asdfdfgh()
      document.querySelector('#updateModal .btn-close').click()
    }
  });
}